local BaseUI = class("BaseUI", function()
  return cc.Layer:create()
end)

function getWndInitParamList()
  local rd = {
    bgImage = nil,
    btnClose = nil,
    imgTitle = nil,
    opensoundPath = nil,
    closesoundPath = nil,
    isImgTitle = true,
    isShowInnerImage = true,
    isShowClose = true,
    isShowCover = true,
    isSwollowTouch = true,
    isShowImage = true,
    isRemoveResource = true,
    isBigWin = true
  }
  return rd
end

function BaseUI:ctor()
  self.m_spCover = nil
  self.m_bgImage = nil
  self.m_btnClose = nil
  self.m_isImgTitle = true
  self.m_isShowClose = true
  self.m_isShowCover = true
  self.m_isSwollowTouch = true
  self.m_isShowImage = true
  self.m_isBigWin = true
  self.m_showFromPosX = nil
  self.m_showFromPosY = nil
  self.m_opensoundPath = "res/sound/ui/btn_close.mp3"
  self.m_closesoundPath = "res/sound/ui/common_window_open.mp3"
end

function BaseUI:initWithParas(paratables)
  self.m_bgImage = paratables.bgImage
  self.m_imgTitle = paratables.imgTitle
  self.m_btnClose = paratables.btnClose
  self.m_isShowClose = paratables.isShowClose
  self.m_isShowCover = paratables.isShowCover
  self.m_isSwollowTouch = paratables.isSwollowTouch
  self.m_isImgTitle = paratables.isImgTitle
  self.m_isBigWin = paratables.isBigWin
  if paratables.opensoundPath then
    self.m_opensoundPath = paratables.opensoundPath
  else
    self.m_opensoundPath = "res/sound/ui/btn_close.mp3"
  end
  if paratables.closesoundPath then
    self.m_closesoundPath = paratables.closesoundPath
  else
    self.m_closesoundPath = "res/sound/ui/common_window_open.mp3"
  end
  if paratables.isShowImage == nil then
    self.m_isShowImage = true
  else
    self.m_isShowImage = paratables.isShowImage
  end
  self.m_spCover = nil
  self.m_showFromPosX = paratables.m_showFromPosX
  self.m_showFromPosY = paratables.m_showFromPosY
  if self.m_isShowCover then
    self.m_spCover = cc.Sprite:create()
    self.m_spCover:setTextureRect(cc.rect(0, 0, WinSize.width, WinSize.height))
    self.m_spCover:setColor(cc.c3b(0, 0, 0))
    self.m_spCover:setOpacity(64)
    self:addChild(self.m_spCover, -10000)
  end
--  if not self.m_bgImage then
--    local spf = "ui/common/window_1.png"
--    self.m_bgImage = cc.Scale9Sprite:createWithSpriteFrameName(spf, cc.rect(50, 50, 570, 570))
--  end
--  self:addChild(self.m_bgImage, -90)
--  self:setContentSize(self.m_bgImage:getContentSize())
--  self:setAnchorPoint(cc.p(0, 0))
--  self.m_bgImage:setVisible(self.m_isShowImage)
--  if not self.m_btnClose then
--    self.m_btnClose = ccui.Button:create("ui/common/btn_close_n.png", "ui/common/btn_close_p.png", "ui/common/btn_close_n.png", 1)
--  end
--  local function clickBaseBtnEvent(sender, eventType)
--    self:clickBaseWindowBtn(sender, eventType)
--  end
--  self:addChild(self.m_btnClose)
--  self.m_btnClose:addTouchEventListener(clickBaseBtnEvent)
--  self.m_btnClose:setVisible(self.m_isShowClose)
--  self.m_btnClose:setTouchEnabled(self.m_isShowClose)
  if self.m_isImgTitle and self.m_imgTitle then
    self:addChild(self.m_imgTitle)
  end
  addEventListenerTouchOneByOne(self, handler(self, self.onTouchBegan), handler(self, self.onTouchMoved), handler(self, self.onTouchEnded), self.m_isSwollowTouch)
  self:layoutBaseWindow()
  self:registerScriptHandler(handler(self, self.onNodeEvent))
end

function BaseUI:layoutBaseWindow()
  if self.m_isBigWin then
    if self.m_btnClose then
      self.m_btnClose:setPosition(cc.p(self.m_bgImage:getContentSize().width / 2 - self.m_btnClose:getContentSize().width / 2 - 20, self.m_bgImage:getContentSize().height / 2 - self.m_btnClose:getContentSize().height / 2 - 40))
    end
    if self.m_imgTitle then
      self.m_imgTitle:setPositionY(self:getContentSize().height / 2 - self.m_imgTitle:getContentSize().height / 2 - 60)
    end
  else
    if self.m_btnClose then
      self.m_btnClose:setPosition(cc.p(self.m_bgImage:getContentSize().width / 2 - self.m_btnClose:getContentSize().width / 2 - 20, self.m_bgImage:getContentSize().height / 2 - self.m_btnClose:getContentSize().height / 2 - 20))
    end
    if self.m_imgTitle then
      self.m_imgTitle:setPositionY(self:getContentSize().height / 2 - self.m_imgTitle:getContentSize().height / 2 - 20)
    end
  end
  self:setPosition(WinSize.width / 2, WinSize.height / 2)
end

function BaseUI:setBaseWindowSize(sizew, sizeh)
  if self.m_bgImage then
    self.m_bgImage:setContentSize(sizew, sizeh)
  end
  self:layoutBaseWindow()
  self:setContentSize(sizew, sizeh)
end

function BaseUI:onNodeEvent(value)
  if value == "exit" then
    self:onExitForSubClass()
    self:unregisterScriptHandler()
  elseif value == "exitTransitionStart" then
  elseif value == "enter" then
    self:onEnterForSubClass()
  end
end

function BaseUI:onExitForSubClass()
end

function BaseUI:onEnterForSubClass()
end

function BaseUI:clickOutOfWindow(touch)
end

function BaseUI:clickInsideWindow(touch)
end

function BaseUI:showWaitingForService(content)
  if self.m_loading then
    self.m_loading:removeFromParent()
    self.m_loading = nil
  end
  self.m_loading = cc.LayerColor:create(cc.c4b(0, 0, 0, 64), self:getContentSize().width, self:getContentSize().height)
  local listener = cc.EventListenerTouchOneByOne:create()
  listener:registerScriptHandler(handler(self, self.onTouchBegan1), cc.Handler.EVENT_TOUCH_BEGAN)
  listener:setSwallowTouches(true)
  self:getEventDispatcher():addEventListenerWithSceneGraphPriority(listener, self.m_loading)
  local loading = require("gameui/LoadingSymbol").create()
  self.m_loading:addChild(loading)
  loading:setPositionX(self.m_loading:getContentSize().width / 2)
  if content then
    local contentlbl = cc.Label:createWithSystemFont(content, DEFAULT_FONT_BOLD, 32, cc.size(600, 0), 1)
    contentlbl:enableOutline(cc.c4b(0, 0, 0, 0), 4)
    contentlbl:enableGlow(cc.c4b(0, 0, 0, 0))
    self.m_loading:addChild(contentlbl)
    contentlbl:setPosition(self.m_loading:getContentSize().width / 2, self.m_loading:getContentSize().height / 2 - contentlbl:getContentSize().height / 2 - 10)
    loading:setPositionY(self.m_loading:getContentSize().height / 2 + loading:getContentSize().height / 2 + 10)
  else
    loading:setPositionY(self.m_loading:getContentSize().height / 2)
  end
  self:addChild(self.m_loading, 10000)
  self.m_loading:setPosition(-self:getContentSize().width / 2, -self:getContentSize().height / 2)
  self.m_loading:registerScriptHandler(handler(self, self.loadingPanelHadle))
end

function BaseUI:onTouchBegan1(touch, event)
  return true
end

function BaseUI:loadingPanelHadle(value)
  if value == "exit" then
    self.m_loading = nil
  end
end

function BaseUI:hideWaitingForService()
  if self.m_loading then
    self.m_loading:removeFromParent()
    self.m_loading = nil
  end
end

function BaseUI:onTouchBegan(touch, event)
  return true
end

function BaseUI:onTouchMoved(touch, event)
end

function BaseUI:onTouchEnded(touch, event)
  if touch:getLocation().x > WinSize.width / 2 + self:getContentSize().width / 2 or touch:getLocation().x < WinSize.width / 2 - self:getContentSize().width / 2 or touch:getLocation().y > WinSize.height / 2 + self:getContentSize().height / 2 or touch:getLocation().y < WinSize.height / 2 - self:getContentSize().height / 2 then
    self:clickOutOfWindow(touch)
  else
    self:clickInsideWindow(touch)
  end
end

function BaseUI:getUILayer()
  return self
end

function BaseUI:clickBaseWindowBtn(sender, eventType)
  if eventType == ccui.TouchEventType.ended and sender == self.m_btnClose then
    cc_playEffect(self.m_closesoundPath, false)
    self:closeWindow()
  end
end

function BaseUI:showWindow(px, py)
  if px and py then
    self.m_showFromPosX = px
    self.m_showFromPosY = py
  end
  if self then
    cc.Director:getInstance():getRunningScene():addChild(self)
    self:openEffectFunction()
    cc_playEffect(self.m_opensoundPath, false)
  end
end

function BaseUI:openEffectFunction()
  if self.m_showFromPosX and self.m_showFromPosY then
    if self.m_spCover then
      self.m_spCover:setVisible(false)
    end
    self:setScale(0)
    self:setPosition(cc.p(self.m_showFromPosX, self.m_showFromPosY))
    local sc = cc.ScaleTo:create(0.15, 1)
    local mt = cc.MoveTo:create(0.15, cc.p(WinSize.width / 2, WinSize.height / 2))
    local fun = cc.CallFunc:create(function()
      self:endShowEffect()
    end)
    self:runAction(cc.Sequence:create(cc.Spawn:create(sc, mt), fun))
  end
end

function BaseUI:endShowEffect()
  if self.m_spCover ~= nil then
    self.m_spCover:setVisible(true)
  end
end

function BaseUI:closeWindow()
  if not self:closeEffectFunction() then
    self:removeWindow()
  end
end

function BaseUI:removeWindow()
  self:removeFromParent()
end

function BaseUI:closeEffectFunction()
  if self.m_showFromPosX and self.m_showFromPosY then
    if self.m_spCover then
      self.m_spCover:setVisible(false)
    end
    local sc = cc.ScaleTo:create(0.15, 0)
    local mt = cc.MoveTo:create(0.15, cc.p(self.m_showFromPosX, self.m_showFromPosY))
    local fun = cc.CallFunc:create(function()
      self:endHideEffect()
    end)
    self:runAction(cc.Sequence:create(cc.Spawn:create(sc, mt), fun))
    return true
  end
  return false
end

function BaseUI:endHideEffect()
  self:removeFromParent()
end

function BaseUI:showCloseBtn(isshow)
  self.m_isShowClose = isshow
  self.m_btnClose:setVisible(isshow)
  self.m_btnClose:setTouchEnabled(isshow)
end

return BaseUI
